(function () {
	// 获取合同列表页面数据 服务器分页
    var serverPlanTaskDetailData = {};
    var serverPlanTaskDetailTable;//泵
    var serverPlanTaskDetailReplaceTable;//备件
    var serverPlanTaskDetailReplaceData = {};
    var plantaskId = jQuery('#plantaskId').val();
    serverPlanTaskDetailData.id = plantaskId;
    serverPlanTaskDetailReplaceData.id = plantaskId;
    jQuery(document).ready(function () {
    	serverPlanTaskDetailTable = jQuery('#PlanTaskDetailTable').DataTable({
    		"serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/scheduling/getPlanTaskDetail',
                "data": function (d) {
                    return $.extend({}, d, serverPlanTaskDetailData);
                }
            },
            columns: [
                  {
                  	data: null
                  },
                  {
                      data: 'cProductName'
                  },
                  {
                      data: 'cProductType'
                  },
                  {
                      data: 'cProductModel'
                  },
                  {
                      data: 'nCount'
                  },
                  {
                  	data: 'nSinglePrice'
                  },
                  {
                      data: 'cPower'
                  },
                  {
                      data: 'cMaterialFlow'
                  },
                  {
                      data: 'cTransMedium'
                  },
                  {
                      data: 'cViscosity'
                  },
                  {
                      data: 'cFlow'
                  },
                  {
                      data: 'cHead'
                  },
                  {
                      data: 'cTemperature'
                  },
                  {
                      data: 'cProportion'
                  },
                  {
                      data: 'cInletPressure'
                  },
                  {
                      data: 'cNPSHa'
                  },
                  {
                      data: 'cNPSHr'
                  },
                  {
                      data: 'cMotor',
                      render:function(data,type,full,meta){
  	                	if(data == null || data == undefined || data == ""){
  							return data;
  						}else{
  							return (data.length > 18) ? '<font title="'+data+'">'+data.substring(0,18)+'...'+'</font>' : data;
  						}
  					}
                  },
                  {
                      data: 'dGiveDate',
                      render: function (data, type, full, meta) {
                          if (data)
                              return moment(data).format('YYYY/MM/DD');
                          else
                              return '';
                      }
                  },
                  {
                	  data: 'cOutletPressure'
                  },
                  {
                	  data: 'cBitNumber'
                  },
                  {
                	  data: 'cUnit'
                  },
                  {
                	  data: 'cAdditionClaim',
                      render:function(data,type,full,meta){
    	                	if(data == null || data == undefined || data == ""){
    							return data;
    						}else{
    							return (data.length > 18) ? '<font title="'+data+'">'+data.substring(0,18)+'...'+'</font>' : data;
    						}
    					}
                  }
                  ],
                  "drawCallback": function (oSettings) {
                      var k = 0;
                      var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                      for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                          $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                          k++;
                      }
                  }
    	});
    	
    	serverPlanTaskDetailReplaceTable = jQuery('#PlanTaskDetailReplaceTable').DataTable({
    		"serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/scheduling/getPlanTaskDetailReplace',
                "data": function (d) {
                    return $.extend({}, d, serverPlanTaskDetailReplaceData);
                }
            },
            columns: [
                  {
                  	data: null
                  },
                  {
                      data: 'deviceName'
                  },
                  {
                      data: 'type'
                  },
                  {
                      data: 'material'
                  },
                  {
                  	data: 'unit'
                  },
                  {
                  	data: 'amount'
                  },
                  {
                      data: 'nOneDiscount'
                  },
                  {
                	  data: 'materialCoding'
                  },
                  {
                	  data: 'memo'
                  }
                  ],
              "drawCallback": function (oSettings) {
                  var k = 0;
                  var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                  for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                      $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                      k++;
                  }
              }
    	});
    	
    	jQuery('#IssuedSchedulingBtn').on('click', function(){
    		if(jQuery("#planTaskAtrrForm") && !jQuery("#planTaskAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
            	return;
            };
    		saveContract();
        	var plantaskId = jQuery('#plantaskId').val()
        	if(jQuery('#cLowStockMemo').val()==""){
        		$.springLemonDialog({
    				content: '降库说明不能为空！',// 确认文本
    				clickButton: function (sender, modal, index) {// 按钮事件处理
    					$(this).closeDialog(modal);
    				}
    			});
    			return;
        	}else{
    	    	$.springLemonDialog({
    				content: '确认下达'+jQuery('#cPlanNumber').val()+'？',// 确认文本
    				clickButton: function (sender, modal, index) {// 按钮事件处理
    					if (index == 0) {
    						var url=window.rootPath+"/ui/scheduling/issuedplan"+"?schedulingIds="+ plantaskId;
    						jQuery.ajax({
    							url:encodeURI(url),
    							cache:false,
    							type : "post",
    							dataType:"json",
    							success:function(data){
    								if(data==0){
    		    	            		window.location.href = window.rootPath+'/ui/scheduling/';
    		    	            	}else{
    		    	            		alert("未下达！");
    		    	            	}
    							}
    						});
    					}
    					$(this).closeDialog(modal);
    				}
    			}); 
        	}
        });
        
        
        jQuery('.saveBtn').on('click', function(){
        	saveContract();
        	
        });
        var saveContract = function() {
        	if(jQuery("#planTaskAtrrForm") && !jQuery("#planTaskAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
            	return;
            };
        	var id = jQuery('#plantaskId').val();
        	var dCreateDate = jQuery('#dCreateDate').val();
        	var cPlanNumber = jQuery('#cPlanNumber').val();
        	var cContractNumber = jQuery('#cContractNumber').val();
        	var dOrderDate = jQuery('#dOrderDate').val();
        	var dDeliveryDate = jQuery('#dDeliveryDate').val();
        	var cUserUnit = jQuery('#cUserUnit').val();
        	var nContractManager = jQuery('#nContractManager').val();
        	var cMemo = jQuery('#cMemo').val();
        	var cLowStockMemo = jQuery('#cLowStockMemo').val();
        	var nViolationDeliveryDate = jQuery('#nViolationDeliveryDate').is(':checked') ? 1 : 0;
        	var nVPerformance = jQuery('#nVPerformance').is(':checked') ? 1 : 0;
        	var nVQuality = jQuery('#nVQuality').is(':checked') ? 1 : 0;
        	var nVOther = jQuery('#nVOther').val();
        	var nBPBase = jQuery('#nBPBase').is(':checked') ? 1 : 0;
        	var dBaseDate = jQuery('#dBaseDate').val();
        	var nBPInterface = jQuery('#nBPInterface').is(':checked') ? 1 : 0;
        	var dInterfaceDate = jQuery('#dInterfaceDate').val();
        	var nBPPID = jQuery('#nBPPID').is(':checked') ? 1 : 0;
        	var dPIDDate = jQuery('#dPIDDate').val();
        	var nBPElectricity = jQuery('#nBPElectricity').is(':checked') ? 1 : 0;
        	var dElectricityDate = jQuery('#dElectricityDate').val();
        	var nBPWiring = jQuery('#nBPWiring').is(':checked') ? 1 : 0;
        	var dWiringDate = jQuery('#dWiringDate').val();
        	var nBPControlCubicle = jQuery('#nBPControlCubicle').is(':checked') ? 1 : 0;
        	var dControlCubicleDate = jQuery('#dControlCubicleDate').val();
        	var nTestReport = jQuery('#nTestReport').is(':checked') ? 1 : 0;
        	var dTestReportDate = jQuery('#dTestReportDate').val();
        	var nBPOther = jQuery('#nBPOther').val();
        	var nExJail = jQuery('#nExJail').is(':checked') ? 1 : 0;
        	var nExThirdParty = jQuery('#nExThirdParty').is(':checked') ? 1 : 0;
        	var nExLeav = jQuery('#nExLeav').is(':checked') ? 1 : 0;
        	var nPaintCompany = jQuery('#nPaintCompany').is(':checked') ? 1 : 0;
        	var nPaintUser = jQuery('#nPaintUser').is(':checked') ? 1 : 0;
        	var nPackingOrdinary = jQuery('#nPackingOrdinary').is(':checked') ? 1 : 0;
        	var nPackingExport = jQuery('#nPackingExport').is(':checked') ? 1 : 0;
        	var nPackingUser = jQuery('#nPackingUser').is(':checked') ? 1 : 0;
        	var cOtherRequirement = jQuery('#cOtherRequirement').val();
        	var d = {
        			"id" : id,
        			"dCreateDate" : dCreateDate,
        			"cPlanNumber" : cPlanNumber,
        			"cContractNumber" : cContractNumber,
        			"dOrderDate" : dOrderDate,
        			"dDeliveryDate" : dDeliveryDate,
        			"cUserUnit" : cUserUnit,
        			"nContractManager" : nContractManager,
        			"cMemo" : cMemo,
        			"cLowStockMemo" : cLowStockMemo,
        			"nViolationDeliveryDate" : nViolationDeliveryDate,
        			"nVPerformance" : nVPerformance,
        			"nVQuality" : nVQuality,
        			"nVOther" : nVOther,
        			"nBPBase" : nBPBase,
        			"dBaseDate" : dBaseDate,
        			"nBPInterface" : nBPInterface,
        			"dInterfaceDate" : dInterfaceDate,
        			"nBPPID" : nBPPID,
        			"dPIDDate" : dPIDDate,
        			"nBPElectricity" : nBPElectricity,
        			"dElectricityDate" : dElectricityDate,
        			"nBPWiring" : nBPWiring,
        			"dWiringDate" : dWiringDate,
        			"nBPControlCubicle" : nBPControlCubicle,
        			"dControlCubicleDate" : dControlCubicleDate,
        			"nTestReport" : nTestReport,
        			"dTestReportDate" : dTestReportDate,
        			"nBPOther" : nBPOther,
        			"nExJail" : nExJail,
        			"nExThirdParty" : nExThirdParty,
        			"nExLeav" : nExLeav,
        			"nPaintCompany" : nPaintCompany,
        			"nPaintUser" : nPaintUser,
        			"nPackingOrdinary" : nPackingOrdinary,
        			"nPackingExport" : nPackingExport,
        			"cOtherRequirement" : cOtherRequirement,
        			"nPackingUser" : nPackingUser,
        	};
        	jQuery("#IssuedSchedulingBtn").attr("disabled", true);
        	jQuery(".saveBtn").attr("disabled", true);
        	var url = window.rootPath+"/ui/scheduling/savePlanTask/"+id;
        	jQuery.ajax({
    	            url:encodeURI(url),
    	            cache:false,
    	            data: JSON.stringify(d),
    	            type :"post",
    	            dataType:"json",
    	            contentType:"application/json;charset=utf-8",// 解决返回数据中的中文乱码问题
    	            success:function(data){
    	            	jQuery("#IssuedSchedulingBtn").attr("disabled", false);
    	            	jQuery(".saveBtn").attr("disabled", false);
    	            	
    	            }
        	});
        };
    	
    	
    });
	
    
    
    
})();